package com.sam;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.sam.utils.AsposeUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Sam
 * @Date: 2024-01-04 17:05
 * @Description: wordToPdf demo
 */
public class PoiTlTest {

    public static void main(String[] args) throws Exception {
        File wordFile = resolveTemp();
        String path = System.getProperty("user.dir") + "/file";
        String pdfNow = LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy年MM月dd日");
        //创建空的pdf
        File pdfFile = new File(path + "/order/" + "采购单" + pdfNow + ".pdf");
        //pdf内容转换写入
        AsposeUtil.wordToPdf(wordFile.getAbsolutePath(), pdfFile.getAbsolutePath());
    }

    public static File resolveTemp() throws Exception {
        String path = System.getProperty("user.dir") + "/file";
        Map<String, Object> datas = new HashMap<>();
        datas.put("dept", "采购部");
        datas.put("name", "张三");
        LocalDate date = LocalDate.now();
        datas.put("year", date.getYear());
        datas.put("month", date.getMonthValue());
        datas.put("day", date.getDayOfMonth());
        List<Map<String, Object>> itemList = new ArrayList<>();
        Map<String, Object> itemMap = new HashMap<>();
        itemMap.put("goods", "笔记本电脑");
        itemMap.put("num", "2");
        itemMap.put("cost", "10000");
        itemMap.put("remark", "按需采购");
        itemList.add(itemMap);
        itemMap = new HashMap<>();
        itemMap.put("goods", "台式电脑");
        itemMap.put("num", "1");
        itemMap.put("cost", "8000");
        itemMap.put("remark", "按需采购");
        itemList.add(itemMap);
        datas.put("item", itemList);
        datas.put("total", "18000.00");

        //读取图片
        File file = new File(path + "/1.png");
        BufferedImage bi = ImageIO.read(file);
        int width = bi.getWidth();
        //源图高度
        int height = bi.getHeight();
        datas.put("image", new PictureRenderData(width, height, PictureType.PNG, new FileInputStream(file)));
        System.out.println("datas = " + datas);

        //table原模板[]
        HackLoopTableRenderPolicy hackLoopTableRenderPolicy = new HackLoopTableRenderPolicy("[", "]");
        Configure config = Configure.builder()
                .buildGramer("${", "}")//此处可以变更原模板${}
                .bind("item", hackLoopTableRenderPolicy)//标记表格参数
                .build();

        String sourceFile = path + "/template/采购单.docx";
        String docNow = LocalDateTimeUtil.format(date, "yyyy年MM月dd日");
        String targetFile = path + "/order/采购单" + docNow + ".docx";
        XWPFTemplate template = XWPFTemplate.compile(new FileInputStream(sourceFile), config).render(datas);
        File wordFile = new File(targetFile);
        File parentFile = wordFile.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(wordFile);
        template.write(out);
        out.flush();
        out.close();
        template.close();
        return wordFile;
    }

}
